package com.shanghaionstar.nav;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.common.DriveSegmentInfo;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.adapter.PoiDriveSegmentListAdapter;
import com.shanghaionstar.nav.iview.INavRoutePlanDetailView;
import com.shanghaionstar.nav.presenter.NavRoutePlanDetailPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.List;

/**
 * @author jun chen
 * @ClassName: NavRoutePlanDetailFragment 
 * @date 2016年2月18日 上午9:54:14 
 * @Description: 路线规划详情页面
 */
public class NavRoutePlanDetailFragment extends BaseFragment implements INavRoutePlanDetailView {
	@FindViewById(R.id.route_time)
	private TextView mTimeTv;
	@FindViewById(R.id.route_space)
	private TextView mSpaceTv;
	@FindViewById(R.id.route_info_view)
	private ListView mRouteLv;
	private FragmentManager fragmentManager;

	private AbstractMapActivity mapActivity;
	private NavRoutePlanDetailPresenter mDetailPresenter;

	public static NavRoutePlanDetailFragment newInstance(PoiItemInfo routeStartPoiItemInfo, PoiItemInfo routeEndPoiItemInfo, String timeInfo, String disInfo ) {
		NavRoutePlanDetailFragment fragment = new NavRoutePlanDetailFragment();
		Bundle bundle = new Bundle();
		bundle.putSerializable("routeStartPoiItemInfo", routeStartPoiItemInfo);
		bundle.putSerializable("routeEndPoiItemInfo", routeEndPoiItemInfo);
		bundle.putString("timeInfo", timeInfo);
		bundle.putString("disInfo", disInfo);
		fragment.setArguments(bundle);
		return fragment;
	}


	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mapActivity = (AbstractMapActivity) activity;
		fragmentManager = mapActivity.getSupportFragmentManager();
	}

	@Override
	public int getLayoutId() {
		return R.layout.nav_route_planning_detail;
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if(isFirstLoad()) {
			mDetailPresenter = new NavRoutePlanDetailPresenter(mapActivity.getScheduler(), mapActivity);
			mDetailPresenter.setView(this);
			Bundle bun = getArguments();
			mDetailPresenter.getData(bun, mTimeTv, mSpaceTv);
			mDetailPresenter.initData(mRouteLv);
		}
	}


	@OnClick({R.id.travel_details})
	public void OnClick(View view){
		switch (view.getId()) {
			case R.id.travel_details:
				BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Routeresults_detailclose.getStringValue());
//				mapActivity.showFragment(fragmentManager,NavRoutePlanResultFragment.class,NavRoutePlanResultFragment.class);
				mapActivity.removeCurrentAndShowPrevFragment(fragmentManager,NavRoutePlanDetailFragment.class);
				break;
		}
	}


	@Override
	public void setLoading(boolean isLoading) {

	}

	@Override
	public void onResponseError(OnStarException e) {

	}

	@Override
	public void onReponseSuccess(Object object) {

	}

	@Override
	public Activity onActivity() {
		return mapActivity;
	}

	@Override
	public void setAdapter(List<DriveSegmentInfo> list) {
		final PoiDriveSegmentListAdapter mAdapter = new PoiDriveSegmentListAdapter(list, mapActivity);
		mRouteLv.setAdapter(mAdapter);
	}
}
